package com.shenmazong.user.task;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.shenmazong.user.entity.TbSysUser;
import com.shenmazong.user.mapper.TbSysUserMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.elasticsearch.core.ElasticsearchRestTemplate;
import org.springframework.data.elasticsearch.core.document.Document;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * @author 军哥
 * @version 1.0
 * @description: 定时同步用户信息
 * @date 2022/6/28 16:54
 */

@Component
@EnableScheduling
@Slf4j
public class UserTask {

    @Autowired
    ElasticsearchRestTemplate elasticsearchRestTemplate;

    @Autowired
    TbSysUserMapper tbSysUserMapper;

    private static boolean createUserOk = false;
    private static String addTime = null;

    /**
     * @description 检查并创建索引
     * @author 军哥
     * @date 2022/6/28 16:59
     * @version 1.0
     */
    @Scheduled(fixedDelay = 1000*2)
    public void createUserIndexTask() {
        //--1 判断是否已经OK
        if(createUserOk) {
            return;
        }

        //--2 判断是否存在
        if(elasticsearchRestTemplate.indexOps(TbSysUser.class).exists()) {
            createUserOk = true;
            return;
        }

        //--3 创建索引
        elasticsearchRestTemplate.indexOps(TbSysUser.class).create();

        //--4 手动创建映射
        Document mapping = elasticsearchRestTemplate.indexOps(TbSysUser.class).createMapping();
        elasticsearchRestTemplate.indexOps(TbSysUser.class).putMapping(mapping);
    }

    /**
     * @description 每10秒钟从数据库中同步数据:全量同步
     * @author 军哥
     * @date 2022/6/28 17:07
     * @version 1.0
     */
//    @Scheduled(fixedDelay = 1000*10)
    public void syncUser() {

        // 全量同步
        List<TbSysUser> tbSysUsers = tbSysUserMapper.selectList(null);
        elasticsearchRestTemplate.save(tbSysUsers);
        log.info("全量同步："+new Date().toString());
    }

    /**
     * @description 增量同步
     * @author 军哥
     * @date 2022/6/28 17:12
     * @version 1.0
     */
    @Scheduled(fixedDelay = 1000*60)
    public void syncUserAdd() {

        // 第一次，全量同步
        if(addTime == null) {
            List<TbSysUser> tbSysUsers = tbSysUserMapper.selectList(
                    new QueryWrapper<TbSysUser>().lambda().orderByAsc(TbSysUser::getUpdateTime)
            );
            elasticsearchRestTemplate.save(tbSysUsers);
            log.info("全量同步："+new Date().toString());

            if(tbSysUsers.size() > 0) {
                TbSysUser sysUser = tbSysUsers.get(tbSysUsers.size() - 1);
                Date updateTime = sysUser.getUpdateTime();
                SimpleDateFormat simpleDateFormat = new SimpleDateFormat("YYYY-MM-dd HH:mm:ss");
                addTime = simpleDateFormat.format(updateTime);
                log.info("addTime1:" + addTime);
            }
            return;
        }

        // 增量同步
        log.info("增量同步："+new Date().toString());
        QueryWrapper<TbSysUser> wrapper = new QueryWrapper<>();
        wrapper.lambda().ge(TbSysUser::getUpdateTime, addTime);
        List<TbSysUser> tbSysUsers = tbSysUserMapper.selectList(wrapper);
        elasticsearchRestTemplate.save(tbSysUsers);
        if(tbSysUsers.size() == 0) {
            TbSysUser sysUser = tbSysUsers.get(tbSysUsers.size() - 1);
            Date updateTime = sysUser.getUpdateTime();
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("YYYY-MM-dd HH:mm:ss");
            addTime = simpleDateFormat.format(updateTime);
            log.info("addTime2:" + addTime);

        }
    }

}
